package com.gdpu.project.dao.impl;

import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.gdpu.page.vo.QueryResult;
import com.gdpu.project.dao.EmailDao;
import com.gdpu.project.vo.Email;
import com.gdpu.project.vo.News;


public class EmailDaoImpl extends HibernateDaoSupport implements EmailDao {
	private static final Log log = LogFactory.getLog(EmailDaoImpl.class);
	// property constants
	public static final String RECEIVER = "receiver";
	public static final String SENDER = "sender";
	public static final String EMAIL_TITLE = "emailTitle";
	public static final String EMAIL_INFO = "emailInfo";
	public static final String FILE = "file";
	public static final String READ_FLAG = "readFlag";
	public static final String FSTIME = "fsTime";

	protected void initDao() {
		// do nothing
	}

	/* (non-Javadoc)
	 * @see com.gdpu.project.dao.impl.EmailDao#save(com.gdpu.project.vo.Email)
	 */
	public void save(Email transientInstance) {
		log.debug("saving Email instance");
		try {
			getHibernateTemplate().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.gdpu.project.dao.impl.EmailDao#delete(com.gdpu.project.vo.Email)
	 */
	public void delete(Email persistentInstance) {
		log.debug("deleting Email instance");
		try {
			getHibernateTemplate().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.gdpu.project.dao.impl.EmailDao#findById(java.lang.Integer)
	 */
	public Email findById(java.lang.Integer id) {
		log.debug("getting Email instance with id: " + id);
		try {
			Email instance = (Email) getHibernateTemplate().get(
					"com.gdpu.project.vo.Email", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List<Email> findByExample(Email instance) {
		log.debug("finding Email instance by example");
		try {
			List<Email> results = getHibernateTemplate().findByExample(instance);
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.gdpu.project.dao.impl.EmailDao#findByProperty(java.lang.String, java.lang.Object)
	 */
	public List<Email> findByProperty(String propertyName, Object value) {
		log.debug("finding Email instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from Email as model where model."
					+ propertyName + "= ?";
			return getHibernateTemplate().find(queryString, value);
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}
	public QueryResult<Email> findByProperty(String propertyName, Object value,int firstindex,int maxresult) {
		log.debug("finding Email instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from Email as model where model."
				+ propertyName + "= :propertyName";
			Query query=getSession().createQuery(queryString);
			if(value!=null){
				query.setParameter("propertyName", value);
			}
			if(firstindex!=-1&&maxresult!=-1){
				query.setFirstResult(firstindex).setMaxResults(maxresult);
			}
			QueryResult<Email> queryResult=new QueryResult<Email>();
			queryResult.setResultlist(query.list());
			query=getSession().createQuery("select count(*) from Email as model where model." + propertyName + "= :propertyName");
			if(value!=null){
				query.setParameter("propertyName", value);
			}
			queryResult.setTotalrecord((Long)query.uniqueResult());
			return queryResult;
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}
	
	public QueryResult<Email> findBySender(Object sender,int firstindex,int maxresult) {
		return findByProperty(SENDER, sender,firstindex,maxresult);
	}
	public QueryResult<Email> findByReceiver(Object receiver,int firstindex,int maxresult) {
		return findByProperty(RECEIVER, receiver,firstindex,maxresult);
	}

	public List<Email> findByReceiver(Object receiver) {
		return findByProperty(RECEIVER, receiver);
	}

	public List<Email> findBySender(Object sender) {
		return findByProperty(SENDER, sender);
	}

	/* (non-Javadoc)
	 * @see com.gdpu.project.dao.impl.EmailDao#findByEmailTitle(java.lang.Object)
	 */
	public List<Email> findByEmailTitle(Object emailTitle) {
		return findByProperty(EMAIL_TITLE, emailTitle);
	}

	/* (non-Javadoc)
	 * @see com.gdpu.project.dao.impl.EmailDao#findByEmailInfo(java.lang.Object)
	 */
	public List<Email> findByEmailInfo(Object emailInfo) {
		return findByProperty(EMAIL_INFO, emailInfo);
	}

	/* (non-Javadoc)
	 * @see com.gdpu.project.dao.impl.EmailDao#findByFile(java.lang.Object)
	 */
	public List<Email> findByFile(Object file) {
		return findByProperty(FILE, file);
	}

	/* (non-Javadoc)
	 * @see com.gdpu.project.dao.impl.EmailDao#findByReadFlag(java.lang.Object)
	 */
	public List<Email> findByReadFlag(Object readFlag) {
		return findByProperty(READ_FLAG, readFlag);
	}

	/* (non-Javadoc)
	 * @see com.gdpu.project.dao.impl.EmailDao#findAll()
	 */
	public List<Email> findAll() {
		log.debug("finding all Email instances");
		try {
			String queryString = "from Email";
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	
	public Email merge(Email detachedInstance) {
		log.debug("merging Email instance");
		try {
			Email result = (Email) getHibernateTemplate().merge(
					detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.gdpu.project.dao.impl.EmailDao#attachDirty(com.gdpu.project.vo.Email)
	 */
	public void attachDirty(Email instance) {
		log.debug("attaching dirty Email instance");
		try {
			getHibernateTemplate().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.gdpu.project.dao.impl.EmailDao#attachClean(com.gdpu.project.vo.Email)
	 */
	public void attachClean(Email instance) {
		log.debug("attaching clean Email instance");
		try {
			getHibernateTemplate().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}
	
	@SuppressWarnings("unchecked")
	public List<Email> splitPage(int startRow) {
		log.debug("finding pageNews News instances");
		try {
			DetachedCriteria criteria = DetachedCriteria.forClass(Email.class);
			criteria.addOrder(Order.desc(FSTIME));
			return getHibernateTemplate()
					.findByCriteria(criteria, startRow, 10);
		} catch (RuntimeException re) {
			log.error("finding pageNews News failed", re);
			throw re;
		}
	}
	

	public static EmailDao getFromApplicationContext(ApplicationContext ctx) {
		return (EmailDao) ctx.getBean("EmailDAO");
	}
}